/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) 
    {
      //找到删除节点
        ListNode* slow=head;
        ListNode* fast=head;
        for(int i=0;i<n;i++)
        {
            fast=fast->next;
        }
        while(fast)
        {
            fast=fast->next;
            slow=slow->next;
        }
        //找到删除节点的前结点，进行删除
        ListNode* del=slow;
        if(del==head)
        {
            head=head->next;
        }
        else
        {
            ListNode* cur=head;
            while(cur->next!=del)
            {
                cur=cur->next;
            }
            cur->next=del->next;
        }
        return head;
    }
};
